<!-- 采购报表 退货统计 -->
<template>
<<<<<<< HEAD
  <view class="app-container">
    <!-- 搜索表单 -->
    <view class="search-form">
      <van-cell-group>
        <van-field
          v-model="queryParams.materialName"
          label="物料名称"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter="handleQuery"
        />
        <van-field
          readonly
          clickable
          label="供应商"
          :value="selectedSupplierLabel"
          placeholder="请选择供应商"
          @click="showSupplierPicker = true"
        />
        <van-popup v-model:show="showSupplierPicker" position="bottom">
          <van-picker
            show-toolbar
            :columns="supplierIdOptions"
            value-key="dictLabel"
            @confirm="onSupplierConfirm"
            @cancel="showSupplierPicker = false"
          />
        </van-popup>
        <van-field
          readonly
          clickable
          label="退货日期"
          :value="daterangeReturnDate.join(' 至 ')"
          placeholder="请选择日期范围"
          @click="showDatePicker = true"
        />
        <van-popup v-model:show="showDatePicker" position="bottom">
          <van-date-picker
            type="date"
            title="选择日期范围"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onDateConfirm"
            @cancel="showDatePicker = false"
          />
        </van-popup>
      </van-cell-group>
      <view class="button-group">
        <van-button type="primary" @click="handleQuery">搜索</van-button>
        <van-button plain @click="resetQuery">重置</van-button>
      </view>
    </view>

    <!-- 数据表格 -->
    <view class="table-container">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="loadMore"
      >
        <van-cell v-for="(item, index) in returnList" :key="index" :title="item.materialName">
          <view class="table-row">
            <text>生产批号: {{ item.batchNo }}</text>
            <text>供应商: {{ item.supplierName }}</text>
            <text>退货单号: {{ item.returnNo }}</text>
            <text>型号: {{ item.materialModel }}</text>
            <text>规格: {{ item.materialSpecification }}</text>
            <text>单位: {{ item.materialUnit }}</text>
            <text>单价: {{ item.price }} 元</text>
            <text>订货数量: {{ item.bookQuantity }}</text>
            <text>到货数量: {{ item.arriveQuantity }}</text>
            <text>退货数量: {{ item.returnQuantity }}</text>
            <text>退货日期: {{ formatDate(item.returnDate) }}</text>
          </view>
        </van-cell>
      </van-list>
    </view>

    <!-- 导出按钮 -->
    <view class="export-button">
      <van-button type="warning" plain @click="handleExport">导出</van-button>
    </view>
  </view>
</template>

<script>
import { ref, reactive, onMounted } from "vue";
import dayjs from "dayjs";

// 模拟 API 数据
const mockApi = {
  listReturn: (params) => {
    const data = Array.from({ length: 10 }, (_, i) => ({
      materialName: `物料-${i + 1}`,
      batchNo: `批次-${i + 1}`,
      supplierName: `供应商-${i + 1}`,
      returnNo: `退货单-${i + 1}`,
      materialModel: `型号-${i + 1}`,
      materialSpecification: `规格-${i + 1}`,
      materialUnit: "个",
      price: Math.floor(Math.random() * 1000),
      bookQuantity: Math.floor(Math.random() * 100),
      arriveQuantity: Math.floor(Math.random() * 100),
      returnQuantity: Math.floor(Math.random() * 50),
      returnDate: new Date(),
    }));
    return new Promise((resolve) => {
      setTimeout(() => resolve({ rows: data, total: data.length }), 500);
    });
  },
  exportReturn: () => {
    return new Promise((resolve) => {
      setTimeout(() => resolve({ msg: "导出成功" }), 500);
    });
  },
};

export default {
  setup() {
    // 查询参数
    const queryParams = reactive({
      pageNum: 1,
      pageSize: 10,
      materialName: "",
      supplierId: null,
      returnDate: [],
    });

    // 数据列表
    const returnList = ref([]);
    const loading = ref(false);
    const finished = ref(false);

    // 退货日期选择器
    const daterangeReturnDate = ref([]);
    const showDatePicker = ref(false);
    const minDate = new Date(2020, 0, 1);
    const maxDate = new Date();

    // 供应商选择器
    const supplierIdOptions = ref([
      { dictValue: 1, dictLabel: "供应商A" },
      { dictValue: 2, dictLabel: "供应商B" },
      { dictValue: 3, dictLabel: "供应商C" },
    ]);
    const selectedSupplierLabel = ref("");
    const showSupplierPicker = ref(false);

    // 获取数据
    const getList = async () => {
      loading.value = true;
      const params = { ...queryParams };
      if (daterangeReturnDate.value.length === 2) {
        params.beginReturnDate = daterangeReturnDate.value[0];
        params.endReturnDate = daterangeReturnDate.value[1];
      }
      const response = await mockApi.listReturn(params);
      returnList.value = [...returnList.value, ...response.rows];
      finished.value = returnList.value.length >= response.total;
      loading.value = false;
    };

    // 加载更多
    const loadMore = () => {
      if (!finished.value) {
        queryParams.pageNum++;
        getList();
      }
    };

    // 搜索
    const handleQuery = () => {
      queryParams.pageNum = 1;
      returnList.value = [];
      finished.value = false;
      getList();
    };

    // 重置
    const resetQuery = () => {
      queryParams.materialName = "";
      queryParams.supplierId = null;
      daterangeReturnDate.value = [];
      selectedSupplierLabel.value = "";
      handleQuery();
    };

    // 导出
    const handleExport = async () => {
      try {
        const response = await mockApi.exportReturn(queryParams);
        uni.showToast({
          title: response.msg,
          icon: "success",
        });
      } catch (error) {
        uni.showToast({
          title: "导出失败",
          icon: "none",
        });
      }
    };

    // 日期选择确认
    const onDateConfirm = (event) => {
      daterangeReturnDate.value = event.map((item) => dayjs(item).format("YYYY-MM-DD"));
      showDatePicker.value = false;
    };

    // 供应商选择确认
    const onSupplierConfirm = (event) => {
      queryParams.supplierId = event.dictValue;
      selectedSupplierLabel.value = event.dictLabel;
      showSupplierPicker.value = false;
    };

    // 格式化日期
    const formatDate = (date) => dayjs(date).format("YYYY-MM-DD");

    // 初始化
    onMounted(() => {
      getList();
    });

    return {
      queryParams,
      returnList,
      loading,
      finished,
      daterangeReturnDate,
      showDatePicker,
      minDate,
      maxDate,
      supplierIdOptions,
      selectedSupplierLabel,
      showSupplierPicker,
      getList,
      loadMore,
      handleQuery,
      resetQuery,
      handleExport,
      onDateConfirm,
      onSupplierConfirm,
      formatDate,
    };
  },
};
</script>

<style lang="scss">
.app-container {
  background-color: #f5f5f5;
  padding: 20px;

  .search-form {
    margin-bottom: 20px;
  }

  .button-group {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;

    .van-button--primary {
      background-color: #4caf50; // 双碳主题绿色
      border-color: #4caf50;
    }

    .van-button--plain {
      color: #4caf50;
      border-color: #4caf50;
    }
  }

  .table-container {
    background-color: #fff;
    border-radius: 8px;
    padding: 10px;

    .table-row {
      display: flex;
      flex-direction: column;
      gap: 5px;
      margin-bottom: 10px;

      text {
        font-size: 14px;
        color: #333;
      }
    }
  }

  .export-button {
    margin-top: 20px;
    text-align: center;

    .van-button--warning {
      background-color: #ff9800; // 警告色
      border-color: #ff9800;
    }

    .van-button--plain {
      color: #ff9800;
      border-color: #ff9800;
    }
  }
}
</style>
=======
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" clearable filterable >
          <el-option v-for="dict in supplierIdOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="供应商" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商"
          clearable

          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="退货日期">
        <el-date-picker v-model="daterangeReturnDate"  style="width: 240px" value-format="yyyy-MM-dd"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary"   @click="handleQuery">搜索</el-button>
        <el-button    @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button  type="warning" plain   @click="handleExport">导出</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="returnList">
      <el-table-column label="物料名称" align="center" prop="materialName"  width="200" />
      <el-table-column label="生产批号" align="center" prop="batchNo"  width="200" />
      <el-table-column label="供应商" align="center" prop="supplierName"  width="200" />
      <el-table-column label="退货单号" align="center" prop="returnNo"  width="200" />
      <el-table-column label="型号" align="center" prop="materialModel"  width="200" />
      <el-table-column label="规格" align="center" prop="materialSpecification"  width="200" />
      <el-table-column label="单位" align="center" prop="materialUnit"  width="200" />
      <el-table-column label="单价" align="center" prop="price"  width="200" />
      <el-table-column label="订货数量" align="center" prop="bookQuantity"  width="200" />
      <el-table-column label="到货数量" align="center" prop="arriveQuantity"  width="200" />
      <el-table-column label="退货数量" align="center" prop="returnQuantity"  width="200" />
      <el-table-column label="退货日期" align="center" prop="returnDate"  width="200" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
  import {
    listReturn,
    exportReturn
  } from "@/api/purchaseReport/return";

  export default {
    name: "Return",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 采购退货统计表格数据
        returnList: [],
        // 供应商id字典
        supplierIdOptions: [],
        // 退货日期时间范围
        daterangeReturnDate: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          materialId: null,
          batchNo: null,
          materialName: null,
          bookQuantity: null,
          arriveQuantity: null,
          supplierId: null,
          returnDate: null,
          supplierName: null,
          returnNo: null
        },
      };
    },
    created() {
      this.getList();
      this.getQueryData("query_supplier").then(res => {
        this.supplierIdOptions = res.data;
      });
    },
    methods: {
      /** 查询采购退货统计列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeReturnDate && '' != this.daterangeReturnDate) {
          this.queryParams.params["beginReturnDate"] = this.daterangeReturnDate[0];
          this.queryParams.params["endReturnDate"] = this.daterangeReturnDate[1];
        }
        listReturn(this.queryParams).then(response => {
          this.returnList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 供应商id字典翻译
      supplierIdFormat(row, column) {
        return this.selectDictLabel(this.supplierIdOptions, row.supplierId);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeReturnDate = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有采购退货统计数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportReturn(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }

  };

</script>
>>>>>>> 5b8858b09e35d379956227f2720ff4c2cd0dbd08
